package cn.dong.order.controller.api;


import cn.dong.api.domain.CommonResult;
import cn.dong.api.domain.ResultCodeEnum;
import cn.dong.api.utils.JwtInfo;
import cn.dong.api.utils.JwtUtils;
import cn.dong.base.dto.TradeOrderDto;
import cn.dong.base.handler.CustomException;
import cn.dong.order.domain.TradeOrder;
import cn.dong.order.service.TradeOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/api/order/trade-order")
public class TradeOrderController {


    @Autowired
    private TradeOrderService tradeOrderService;

    /**
     * 创建订单
     *
     * @return
     */
    @PostMapping("/auth/createOrder/{courseId}")
    public CommonResult create(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(40000, "您还没有登录呢，请先登录！");
        }
        String order = tradeOrderService.createOrder(courseId, jwtInfo.getId());
        return CommonResult.ok().data(order);
    }

    /**
     * 获取订单信息
     *
     * @return
     */
    @GetMapping("/auth/getOrder/{orderId}")
    public CommonResult getOrder(@PathVariable("orderId") String orderId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        // 获取订单信息
        return CommonResult.ok().data(tradeOrderService.getById(orderId));
    }


    /**
     * 获取订单状态，根据课程id获取
     *
     * @param courseId
     * @param request
     * @return
     */
    @GetMapping("/auth/getOrderState/{courseId}")
    public CommonResult getOrderState(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        // 获取订单状态
        QueryWrapper<TradeOrder> wrapper = new QueryWrapper<TradeOrder>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", jwtInfo.getId());
        wrapper.eq("status", 1);
        return CommonResult.ok().data(tradeOrderService.count(wrapper) > 0);
    }

    /**
     * 根据当前用户查询全部的订单信息，返回对象
     *
     * @param id
     * @return
     */
    @GetMapping("/inner/getOrderByMemberId/{id}")
    public List<TradeOrderDto> getOrderByMemberId(@PathVariable("id") String id) {
        List<TradeOrderDto> list = tradeOrderService.getOrderByMemberId(id);
        return list;
    }

    /**
     * 删除订单
     * @return
     */
    @DeleteMapping("/auth/deleteOrder/{orderId}")
    public CommonResult deleteOrder(@PathVariable String orderId, HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        QueryWrapper<TradeOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orderId);
        wrapper.eq("member_id", jwtInfo.getId());
        return CommonResult.whether(tradeOrderService.remove(wrapper));
    }
}

